package top.ed333.mcplugins.configurableevents.handler;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.plugin.EventExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.ed333.mcplugins.configurableevents.action.Action;
import top.ed333.mcplugins.configurableevents.action.Actions;
import top.ed333.mcplugins.configurableevents.requirement.ReqGroups;

public class HandlerImpl implements Handler {

    private final String name;
    private final String usePackage;
    private final Actions actions;
    private final ReqGroups reqGroups;
    private final EventPriority priorityBukkit;
    private final String eventName;

    private final Class<? extends Event> eventClass;

    public HandlerImpl(
            @NotNull String name,
            @NotNull String usePackage,
            @NotNull String eventName,
            @NotNull EventPriority priority,
            @Nullable ReqGroups reqGroups,
            @Nullable Actions actions,
            @NotNull Class<? extends Event> eventClass
    ) {
        this.name = name;
        this.usePackage = usePackage;
        this.eventName = eventName;
        this.priorityBukkit = priority;
        this.reqGroups = reqGroups;
        this.actions = actions;
        this.eventClass = eventClass;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String usePackage() {
        return usePackage;
    }

    @Override
    public String eventName() {
        return eventName;
    }

    @Override
    public String className() {
        return usePackage + "." + eventName;
    }

    @Override
    public EventPriority priorityBukkit() {
        return priorityBukkit;
    }

    @Override
    public ReqGroups requirement() {
        return reqGroups;
    }

    @Override
    public Actions actions() {
        return actions;
    }

    @Override
    public Class<? extends Event> getEventClass() {
        return eventClass;
    }

    @Override
    public EventExecutor getAsEventExecutor() {
        return (listener, event) -> {
            if (eventClass.isAssignableFrom(event.getClass())) {
                if (event instanceof Cancellable && ((Cancellable) event).isCancelled()) return;
                if (reqGroups != null && !reqGroups.checkReqs(event)) return;

                executeAllActions(event);
            }
        };
    }

    public void executeAllActions(Event event) {
        if (actions == null) return;

        if (event instanceof PlayerEvent) {
            Player targetPlayer = ((PlayerEvent) event).getPlayer();
            System.out.println("targetPlayer: " + targetPlayer);
            for (Action action : actions) {
                action.execute(targetPlayer);
            }
       } else {
            for (Action action : actions) {
                action.execute(null);
            }
        }
    }
}
